//
//  ViewController.m
//  dianfan
//
//  Created by Ye Jin on 15/11/8.
//  Copyright © 2015年 Ye Jin. All rights reserved.
//

#import "ViewController.h"
#import <BmobSDK/Bmob.h>
#import "RegisterVC.h"
#import "MBProgressHUD.h"

@interface ViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *account;
@property (weak, nonatomic) IBOutlet UITextField *passWord;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.account.delegate = self;
    self.account.returnKeyType = UIReturnKeyDone;
    self.passWord.delegate = self;
    self.passWord.returnKeyType = UIReturnKeyDone;
    [self.passWord setSecureTextEntry:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
        if ([bUser.username isEqual: @"admin"]) {
            //跳转管理员页面
            [self performSegueWithIdentifier:@"admin" sender:self];
        }
        else
        {
            //跳转到点饭页面
            [self performSegueWithIdentifier:@"customer1" sender:self];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginBtn:(id)sender
{
    //转菊花
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    hud.labelText=@"loading";
    [BmobUser loginWithUsernameInBackground:self.account.text
                                   password:self.passWord.text block:^(BmobUser *user, NSError *error) {
                                       if (user)
                                       {
                                           [MBProgressHUD hideHUDForView:self.view animated:YES];
                                           [self.account resignFirstResponder];
                                           [self.passWord resignFirstResponder];
                                           UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录成功" message:nil delegate:self   cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                                           [alert show];
                                           if ([user.username isEqual: @"admin"]) {
                                               //跳转管理员页面
                                               [self performSegueWithIdentifier:@"admin" sender:self];
                                           }
                                           else
                                           {
                                               //跳转到点饭页面
                                               [self performSegueWithIdentifier:@"customer1" sender:self];
                                           }
                                       }
                                       else if (error)
                                        {
                                            
                                            BmobQuery *query = [BmobUser query];
                                            [query whereKey:@"username" equalTo:self.account.text];
                                            [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                                                if (error) {
                                                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                                                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@",[[error userInfo] objectForKey:@"error"]] message:nil  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                                                    [alert show];
                                                }
                                                else
                                                {
                                                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                                                    if ([array count] == 0) {
                                                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"用户不存在" message:nil     delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                                                        [alert show];
                                                    }
                                                    else
                                                    {
                                                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"密码错误" message:nil  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                                                        [alert show];
                                                    }
                                                }
                                            }];
                                        }
                                       else
                                       {
                                           UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"unknow error" message:nil  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                                           [alert show];
                                       }
                                       self.account.text = @"";
                                       self.passWord.text = @"";
                                   }];
}

- (IBAction)registerBtn:(id)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    RegisterVC *rvc = [sb instantiateViewControllerWithIdentifier:@"register"];
    [self presentViewController:rvc animated:YES completion:nil];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //每次按下return都进行登录
    [BmobUser loginWithUsernameInBackground:self.account.text
                                   password:self.passWord.text];
    //实现键盘往下收
    [textField resignFirstResponder];
    return YES;
}

//点击屏幕空白处去掉键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.account resignFirstResponder];
    [self.passWord resignFirstResponder];
}

@end
